home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
CD-ROM Today - The Disc! 5
/
CD-ROM Today - The Disc (Issue 5)(November 1994).ISO
/
mac
/
Mac shareware
/
Education
/
RLaB
/
test
< prev
next >
Wrap
Text File
|
1994-09-21
|
47KB
|
1,822 lines
tic();
//
// RLaB test program
//
//
// Test Parameters and some functions we will need later
//
X = 3;
epsilon = function()
{
eps = 1.0;
while((eps + 1.0) != 1.0)
{
eps = eps/2.0;
}
return eps;
};
eps = epsilon();
eye = function( m , n )
{
local(i, N, new);
if (!exist (n))
{
if(m.n != 2) { error("only 2-el MATRIX allowed as eye() arg"); }
new = zeros (m[1], m[2]);
N = min ([m[1], m[2]]);
else
if (class (m) == "string" || class (n) == "string") {
error ("eye(), string arguments not allowed");
}
if (max (size (m)) == 1 && max (size (n)) == 1)
{
new = zeros (m[1], n[1]);
N = min ([m[1], n[1]]);
else
error ("matrix arguments to eye() must be 1x1");
}
}
for(i in 1:N)
{
new[i;i] = 1.0;
}
return new;
};
symm = function( A )
{
return (A + A')./2;
};
//-------------------------------------------------------------------//
// Synopsis: Pascal matrix.
// Syntax: P = pascal ( N )
// Description:
// The Pascal matrix of order N: a symmetric positive definite
// matrix with integer entries taken from Pascal's triangle. The
// Pascal matrix is totally positive and its inverse has integer
// entries. Its eigenvalues occur in reciprocal pairs. COND(P)
// is approximately 16^N/(N*PI) for large N. PASCAL(N,1) is the
// lower triangular Cholesky factor (up to signs of columns) of
// the Pascal matrix. It is involutary (is its own
// inverse). PASCAL(N,2) is a transposed and permuted version of
// PASCAL(N,1) which is a cube root of the identity.
// References:
// R. Brawer and M. Pirovino, The linear algebra of the Pascal matrix,
// Linear Algebra and Appl., 174 (1992), pp. 13-23 (this paper
// gives a factorization of L = PASCAL(N,1) and a formula for the
// elements of L^k).
// S. Karlin, Total Positivity, Volume 1, Stanford University Press,
// 1968. (Page 137: shows i+j-1 choose j is TP (i,j=0,1,...).
// PASCAL(N) is a submatrix of this matrix.)
// M. Newman and J. Todd, The evaluation of matrix inversion programs,
// J. Soc. Indust. Appl. Math., 6(4):466--476, 1958.
// H. Rutishauser, On test matrices, Programmation en Mathematiques
// Numeriques, Editions Centre Nat. Recherche Sci., Paris, 165,
// 1966, pp. 349-365. (Gives an integral formula for the
// elements of PASCAL(N).)
// J. Todd, Basic Numerical Mathematics, Vol. 2: Numerical Algebra,
// Birkhauser, Basel, and Academic Press, New York, 1977, p. 172.
// H.W. Turnbull, The Theory of Determinants, Matrices, and Invariants,
// Blackie, London and Glasgow, 1929. (PASCAL(N,2) on page 332.)
// This file is a translation of pascal.m from version 2.0 of
// "The Test Matrix Toolbox for Matlab", described in Numerical
// Analysis Report No. 237, December 1993, by N. J. Higham.
//-------------------------------------------------------------------//
pascal = function ( n , k )
{
local(P, i, j, k, n)
if (!exist (k)) { k = 0; }
P = diag( (-1).^[0:n-1] );
P[; 1] = ones(n,1);
// Generate the Pascal Cholesky factor (up to signs).
for (j in 2:n-1)
{
for (i in j+1:n)
{
P[i;j] = P[i-1;j] - P[i-1;j-1];
}
}
if (k == 0)
{
P = P*P';
else if (k == 2) {
P = rot90(P,3);
if (n/2 == round(n/2)) { P = -P; }
}}
return P;
};
//
//-------------------- Test Relational Expressions -------------------
//
printf("\tstart scalar tests...\n");
printf("\tstart relational tests...\n");
// SCALAR CONSTANTS (REAL)
if( !(1<2) ) { error(); }
if( !(1<=2) ) { error(); }
if( 1>2 ) { error(); }
if( 1>=2 ) { error(); }
if( 1==2 ) { error(); }
if( !(1!=2) ) { error(); }
if( !1 ) { error(); }
if( !!!1) { error(); }
if( !([1]<[2]) ) { error(); }
if( !([1]<=[2]) ) { error(); }
if( [1]>[2] ) { error(); }
if( [1]>=[2] ) { error(); }
if( [1]==[2] ) { error(); }
if( !([1]!=[2]) ) { error(); }
if( ![1] ) { error(); }
if( !!![1]) { error(); }
// SCALAR CONSTANTS (COMPLEX)
if( !(1+2i<2+3i) ) { error(); }
if( !(1+2i<=2+3i) ) { error(); }
if( 1+2i>2+3i ) { error(); }
if( 1+2i>=2+3i ) { error(); }
if( 1+2i==2+3i ) { error(); }
if( !(1+2i!=2+3i) ) { error(); }
if( !1+2i ) { error(); }
if( !!!1+2i) { error(); }
if( !([1+2i]<[2+3i]) ) { error(); }
if( !([1+2i]<=[2+3i]) ) { error(); }
if( [1+2i]>[2+3i] ) { error(); }
if( [1+2i]>=[2+3i] ) { error(); }
if( [1+2i]==[2+3i] ) { error(); }
if( !([1+2i]!=[2+3i]) ) { error(); }
if( ![1+2i] ) { error(); }
if( !!![1+2i]) { error(); }
// SCALAR ENTITIES (REAL)
a=1;b=2;
if( !(a<b) ) { error(); }
if( !(a<=b) ) { error(); }
if( a>b ) { error(); }
if( a>=b ) { error(); }
if( a==b ) { error(); }
if( !(a!=b) ) { error(); }
if( !a ) { error(); }
if( !!!a) { error(); }
if( !([a]<[b]) ) { error(); }
if( !([a]<=[b]) ) { error(); }
if( [a]>[b] ) { error(); }
if( [a]>=[b] ) { error(); }
if( [a]==[b] ) { error(); }
if( !([a]!=[b]) ) { error(); }
if( ![a] ) { error(); }
if( !!![a]) { error(); }
// SCALAR ENTITIES (COMPLEX)
a=1+2i;b=2+3i;
if( !(a<b) ) { error(); }
if( !(a<=b) ) { error(); }
if( a>b ) { error(); }
if( a>=b ) { error(); }
if( a==b ) { error(); }
if( !(a!=b) ) { error(); }
if( !a ) { error(); }
if( !!!a) { error(); }
if( !([a]<[b]) ) { error(); }
if( !([a]<=[b]) ) { error(); }
if( [a]>[b] ) { error(); }
if( [a]>=[b] ) { error(); }
if( [a]==[b] ) { error(); }
if( !([a]!=[b]) ) { error(); }
if( ![a] ) { error(); }
if( !!![a]) { error(); }
if (! all (all (-2 < rand(4,4)))) { error (); }
if (! all (all (-2 <= rand(4,4)))) { error (); }
if (! all (all ( 2 > rand(4,4)))) { error (); }
if (! all (all ( 2 >= rand(4,4)))) { error (); }
if (! all (all (rand(4,4) > -2))) { error (); }
if (! all (all (rand(4,4) >= -2))) { error (); }
if (! all (all (rand(4,4) < 2))) { error (); }
if (! all (all (rand(4,4) <= 2))) { error (); }
if (! all (all (rand (4,4) > -rand (4,4)))) { error (); }
if (! all (all (rand (4,4) >= -rand (4,4)))) { error (); }
if (! all (all (-rand (4,4) < rand (4,4)))) { error (); }
if (! all (all (-rand (4,4) <= rand (4,4)))) { error (); }
//------------------------- Test REAL SCALARS ------------------------
//
// CONSTANTS
// Addition
if(1+2 != 3) { error(); }
// Subtraction
if(1-2 != -1) { error(); }
// Multiply
if(1*2 != 2) { error(); }
// Divide
if(1/2 != 0.5) { error(); }
// Power
if(2^2 != 4) { error(); }
if(4^0 != 1) { error(); }
// Unary Minus
if(2-3 != -1) { error(); }
//
// ENTITIES
//
a = 1; b = 2; c = 3; d = 0.5;
// Addition
if(a+b != c) { error(); }
// Subtraction
if(a-b != -a) { error(); }
// Multiply
if(a*b != b) { error(); }
// Divide
if(a/b != d) { error(); }
// Power
if(b^b != b*b) { error(); }
if(b^0 != 1) { error (); }
// Unary Minus
if(b-c != -a) { error(); }
//
// ENTITIES & CONSTANTS
//
if(a+2 != c) { error(); }
// Subtraction
if(1-b != -a) { error(); }
// Multiply
if(a*2 != 2) { error(); }
// Divide
if(1/b != d) { error(); }
// Power
if(2^b != b*b) { error(); }
// Unary Minus
if(b-3 != -a) { error(); }
//
//------------------------Test COMPLEX SCALARS -------------------------
//
// CONSTANTS
if(sqrt(-1) != 1i) { error(); }
// Addition
if((1+2i)+(2+3i) != (3+5i)) { error(); }
// Subtraction
if((1+2i)-(3+4i) != (-2-2i)) { error(); }
// Multiply
if((1+2i)*(3+4i) != (-5+10i)) { error(); }
// Divide
if((1+2i)/(3-4i) != (-.2+.4i)) { error(); }
// Power
// Precision problems prevent us from testing these. Have to
// be checked by hand.
// (1+2i)^2 = -3 + 4i
// (1+2i)^.5 = 1.272 + 7.862e-1i
// if((1+2i)^2 != (-3+4i)) { error(); }
// if((1+2i)^10 != (237+3116i)) { error(); }
// Unary Minus
if(-(1+2i) != -1-2i) { error(); }
//
// ENTITIES
//
a = 1+2i; b = 3+4i; c = 4+6i;
if(a+b != c) { error(); }
// Subtraction
if(a-b != -2-2i) { error(); }
// Multiply
if(a*b != -5+10i) { error(); }
// Divide
//if(a/(3-4i) != -.2+.4i) { error(); }
// Power
// if(b^b != b*b) { error(); }
// Unary Minus
if(-a != -1-2i) { error(); }
//
// ENTITIES & CONSTANTS
//
if(a+(3+4i) != c) { error(); }
// Subtraction
if((1+2i)-b != -2-2i) { error(); }
// Multiply
if(a*(3+4i) != -5+10i) { error(); }
// Divide
//if(a/(3-4i) != -.2+.4i) { error(); }
// Power
//if(b^b != b*b) { error(); }
// Unary Minus
if(-(1+2i) != -1-2i) { error(); }
//
// String - Numerical Equalities
//
if ((1 == "1")) { error(); }
if (([1] == "1")) { error(); }
if (("1" == 1)) { error(); }
if (("1" == [1])) { error(); }
if (rand(3,3) == "str") { error(); }
if ("str" == rand(3,3)) { error(); }
if (!any (any ((["1", "2"; "3", "4"] == "1") == [1,0;0,0]))) { error(); }
//
//----------------------- Test REAL MATRICES ---------------------------
//
printf("\tstart matrix tests...\n");
printf("\t\treal-matrices\n");
// Read in test matrices
//
read("test.input");
//
// Matrix construction
//
if(all([3] != matrix(3))) {
error();
}
if(any([1;2;3] != [1,2,3]')) {
error();
}
if(any (any (m0 != zeros(2,2)))) { error(); }
if(any (any (m1 != 1+zeros(2,2)))) { error(); }
if(any (any (m2 != [1,2;3,4]))) { error(); }
if(any (any (m3 != [1+2i,2+3i;3+4i,5+6i]))) { error(); }
if(any (any ([1,2;3+0i,4+0i] != m2))) { error(); }
if(any (any ([m2] != m2))) { error(); }
//
// Matrix indexing
//
p = pascal(6);
if (!all (all (p[ [1:3] ; ] == p[ [1:3]' ; ]))) { error (); }
if (!all (all (p[ ; [1:3] ] == p[ ; [1:3]' ]))) { error (); }
if (!all (all (p[ [2:6] ; [2:6] ] == p[ [2:6]'; [2:6]' ]))) { error (); }
if (!all (all (p[ [2:6]' ; [2:6] ] == p[ [2:6]'; [2:6]' ]))) { error (); }
if (!all (all (p[ [6:12]' ] == p[ [6:12] ]))) { error (); }
//
// Sub-Matrix promotion
//
if(m2[2;2] != 4) { error(); }
if(any(m2[2;] != [3,4])) { error(); }
if(any(m2[;2] != [2,4]')) { error(); }
i=2;j=1;
if(m2[i;j] != 3) { error(); }
i=1;j=2;
if(m2[i;j] != 2) { error(); }
m = [1,2,3;4,5,6;7,8,9];
if(any(m[1;1,2] != [1,2])) {
error();
}
if(any(m[1,2;1] != [1;4])) {
error();
}
if(any (any (m[1,2;1,2] != [1,2;4,5]))) {
error();
}
//
if(m3[2;2] != (5+6i)) { error(); }
if(any(m3[2;] != [3+4i,5+6i])) { error(); }
if(any(m3[;2] != conj([2+3i,5+6i]'))) { error(); }
//
// Automatic creation, extension
//
if(any (any ((mm[3;3]=10) != [0,0,0;0,0,0;0,0,10]))) { error(); }
a=[1,2,3;4,5,6];
if(any (any ((a[3;1]=10) != [1,2,3;4,5,6;10,0,0]))) { error(); }
a=[1,2;3,4];
if(any (any ((a[3,4;3,4]=[5,6;7,8]) != [1,2,0,0;3,4,0,0;0,0,5,6;0,0,7,8]))) {
error();
}
mmm[2;] = a[1;];
//
// Matrix binary operations
//
a = m2; b = [5,6;7,8];
if(any (any (a+a != [2,4;6,8]))) { error(); }
if(any (any (a-a != zeros(2,2)))) { error(); }
if(any (any (2+a != [3,4;5,6]))) { error(); }
if(any (any (2-a != [1,0;-1,-2]))) { error(); }
if(any (any (a-2 != [-1,0;1,2]))) { error(); }
if(any (any (2*a != [2,4;6,8]))) { error(); }
if(any (any ((a./2 != [0.5,1;1.5,2])))) { error(); }
if(any (any (a*a != [7,10;15,22]))) { error(); }
if(any (any (a*a*a != [37,54;81,118]))) { error(); }
if(any (any (a .* a != [1,4;9,16]))) { error(); }
if(any (any (a./a != [1,1;1,1]))) { error(); }
if(any (any (a' != [1,3;2,4]))) { error(); }
if(any(any(rand(3,3)^0 != eye(3,3)))) { error(); }
if(any(any(rand(3,3).^0 != ones(3,3)))) { error(); }
if(any(any(rand(1,3).^0 != ones(1,3)))) { error(); }
if(any(any(rand(3,1).^0 != ones(3,1)))) { error(); }
if(any(any(1.^zeros(3,1) != ones(3,1)))) { error(); }
if(any(any(1.^zeros(1,3) != ones(1,3)))) { error(); }
if(any ([1;2;3]+[4;5;6] != [5;7;9])) {
error();
}
if(any ([1;2;3]-[4;5;6] != [-3;-3;-3])) {
error();
}
if(any ([2;2;2] ./ [1;1;1] != [2;2;2])) {
error();
}
if(any ([1;2;3] .* [4;5;6] != [4;10;18])) {
error();
}
//
// Test row-wise matrix addition
//
a = [1,2,3]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
if (!all (all (a .+ b == [2,4,6;5,7,9;8,10,12;11,13,15]))) { error (); }
if (!all (all (b .+ a == [2,4,6;5,7,9;8,10,12;11,13,15]))) { error (); }
a = [1,2,3] + [1,2,3]*1i;
b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
c = [2,4,6;5,7,9;8,10,12;11,13,15] + [2,4,6;5,7,9;8,10,12;11,13,15]*1i;
if (!all (all (a .+ b == c))) { error (); }
if (!all (all (b .+ a == c))) { error (); }
printf("\t\tpassed matrix row-wise add test...\n");
//
// Test row-wise matrix subtraction
//
a = [1,1,1]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
if (!all (all (a .- b == -[0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
if (!all (all (b .- a == [0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
a = [1,1,1] + [1,1,1]*1i;
b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
c = [0,1,2;3,4,5;6,7,8;9,10,11] + [0,1,2;3,4,5;6,7,8;9,10,11]*1i;
if (!all (all (a .- b == -c))) { error (); }
if (!all (all (b .- a == c))) { error (); }
printf("\t\tpassed matrix row-wise subtraction test...\n");
//
// Test col-wise matrix addition
//
a = [1;1;1;1]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
if (!all (all (a .+ b == [2,3,4;5,6,7;8,9,10;11,12,13]))) { error (); }
if (!all (all (b .+ a == [2,3,4;5,6,7;8,9,10;11,12,13]))) { error (); }
a = [1;1;1;1] + [1;1;1;1]*1i;
b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
c = [2,3,4;5,6,7;8,9,10;11,12,13] + [2,3,4;5,6,7;8,9,10;11,12,13]*1i;
if (!all (all (a .+ b == c))) { error (); }
if (!all (all (b .+ a == c))) { error (); }
printf("\t\tpassed matrix col-wise add test...\n");
//
// Test col-wise matrix subtraction
//
a = [1;1;1;1]; b = [1,2,3;4,5,6;7,8,9;10,11,12];
if (!all (all (a .- b == -[0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
if (!all (all (b .- a == [0,1,2;3,4,5;6,7,8;9,10,11]))) { error (); }
a = [1;1;1;1] + [1;1;1;1]*1i;
b = [1,2,3;4,5,6;7,8,9;10,11,12] + [1,2,3;4,5,6;7,8,9;10,11,12]*1i;
c = [0,1,2;3,4,5;6,7,8;9,10,11] + [0,1,2;3,4,5;6,7,8;9,10,11]*1i;
if (!all (all (a .- b == -c))) { error (); }
if (!all (all (b .- a == c))) { error (); }
printf("\t\tpassed matrix col-wise subtraction test...\n");
a = [1,2,3];
b = [1,2,3;4,5,6;7,8,9];
c = [1,4,9;4,10,18;7,16,27];
if (!all (all (a .* b == c))) { error (); }
if (!all (all (b .* a == c))) { error (); }
za = a + rand (size (a))*1j;
zb = b + rand (size (b))*1j;
if (!all (all (za .* zb == [za;za;za] .* zb))) { error (); }
if (!all (all (zb .* za == zb .* [za;za;za]))) { error (); }
if (!all (all (a .* zb == [a;a;a] .* zb))) { error (); }
if (!all (all (zb .* a == zb .* [a;a;a]))) { error (); }
if (!all (all (za .* b == [za;za;za] .* b))) { error (); }
if (!all (all (b .* za == b .* [za;za;za]))) { error (); }
printf("\t\tpassed matrix row-wise multiplication test...\n");
a = [1,2,3];
b = [1,2,3;4,6,6;7,8,9];
c = [1,1,1;4,3,2;7,4,3];
if (!all (all (b ./ a == c))) { error (); }
if (!all (all ([a;a;a] ./ b == a ./ b))) { error (); }
if (!all (all (b ./ [a;a;a] == b ./ a))) { error (); }
za = a + rand (size (a))*1j;
zb = b + rand (size (b))*1j;
if (!all (all ([za;za;za] ./ zb == za ./ zb))) { error (); }
if (!all (all (zb ./ [za;za;za] == zb ./ za))) { error (); }
if (!all (all ([a;a;a] ./ zb == a ./ zb))) { error (); }
if (!all (all (zb ./ [a;a;a] == zb ./ a))) { error (); }
if (!all (all ([za;za;za] ./ b == za ./ b))) { error (); }
if (!all (all (b ./ [za;za;za] == b ./ za))) { error (); }
printf("\t\tpassed matrix row-wise division test...\n");
a = [1;2;3];
b = [1,2,3;4,5,6;7,8,9];
if (!all (all (a .* b == [a,a,a] .* b))) { error (); }
if (!all (all (b .* a == b .* [a,a,a]))) { error (); }
za = a + rand (size (a))*1j;
zb = b + rand (size (b))*1j;
if (!all (all (za .* zb == [za,za,za] .* zb))) { error (); }
if (!all (all (zb .* za == zb .* [za,za,za]))) { error (); }
if (!all (all (za .* b == [za,za,za] .* b))) { error (); }
if (!all (all (b .* za == b .* [za,za,za]))) { error (); }
if (!all (all (a .* zb == [a,a,a] .* zb))) { error (); }
if (!all (all (zb .* a == zb .* [a,a,a]))) { error (); }
printf("\t\tpassed matrix column-wise multiplication test...\n");
a = [1;2;3];
b = [1,2,3;4,6,6;7,8,9];
if (!all (all ([a,a,a] ./ b == a ./ b))) { error (); }
if (!all (all (b ./ [a,a,a] == b ./ a))) { error (); }
za = a + rand (size(a))*1j;
zb = b + rand (size(b))*1j;
if (!all (all ([za,za,za] ./ zb == za ./ zb))) { error (); }
if (!all (all (zb ./ [za,za,za] == zb ./ za))) { error (); }
if (!all (all ([za,za,za] ./ b == za ./ b))) { error (); }
if (!all (all (b ./ [za,za,za] == b ./ za))) { error (); }
if (!all (all ([a,a,a] ./ zb == a ./ zb))) { error (); }
if (!all (all (zb ./ [a,a,a] == zb ./ a))) { error (); }
printf("\t\tpassed matrix column-wise division test...\n");
//
//--------------------- Test COMPLEX MATRICES --------------------------
//
// Automatic creation, extension
//
printf("\t\tcomplex-matrices\n");
if(any (any ((mm[3;3]=10+10i) != [0,0,0;0,0,0;0,0,10+10i]))) { error(); }
a=[1,2,3;4,5,6];
if(any (any ((a[3;1]=10+10i) != [1,2,3;4,5,6;10+10i,0,0]))) { error(); }
//
a = m3;
if(any (any (a+a != [2+4i,4+6i;6+8i,10+12i]))) { error(); }
if(any (any (a-a != zeros(2,2)))) { error(); }
if(any (any (2+a != [3+2i,4+3i;5+4i,7+6i]))) { error(); }
if(any (any (2-a != [1-2i,0-3i;-1-4i,-3-6i]))) { error(); }
if(any (any (a-2 != [-1+2i,0+3i;1+4i,3+6i]))) { error(); }
if(any (any (2*a != [2+4i,4+6i;6+8i,10+12i]))) { error(); }
if(any (any (a./2 != [.5+1i,1+1.5i;1.5+2i,2.5+3i]))) { error(); }
if(any (any (a*a != [-9+21i,-12+34i;-14+48i,-17+77i]))) { error(); }
if(any (any (a*a*a != [-223+57i,-345+113i;-469+183i,-719+337i]))) { error(); }
if(any (any (a .* a != [-3+4i,-5+12i;-7+24i,-11+60i]))) { error(); }
//
// The following test may not work on some machines
//
if(any (any (a./a != [1,1;1,1]))) {
printf("\t\t***complex division inaccuracy, check manually***\n");
}
if(any (any (a' != conj([1+2i,3+4i;2+3i,5+6i])))) { error(); }
//
//--------------------- Test NULL MATRICES -------------------------
//
printf("\t\tnull-matrices\n");
// Create a NULL matrix
mnull = matrix();
// Test it with SCALARS
if( any([1,mnull] != 1)) {
error();
}
if( any([mnull,1] != 1)) {
error();
}
// Test with MATRIX construction
m = [1,2;3,4;5,6];
if( any([mnull;1] != [1])) {
error();
}
if( any([1;mnull] != [1])) {
error();
}
if( any([mnull;1,2,3] != [1,2,3])) {
error();
}
if( any([1,2,3;mnull] != [1,2,3])) {
error();
}
if(any (any ([mnull,m] != m))) {
error();
}
if(any (any ([m,mnull] != m))) {
error();
}
if(any (any ([mnull;m] != m))) {
error();
}
if(any (any ([m;mnull] != m))) {
error();
mnull = matrix();
mnull[1] = [1];
}
//--------------------- Test Matrix Multiply --------------------------
i = sqrt(-1);
a = [1,2,3;4,5,6;7,8,9];
b = [4,5,6;7,8,9;10,11,12];
c = [ 48, 54, 60 ;
111, 126, 141 ;
174, 198, 222 ];
if (any (any (c != a*b))) { error ("failed Real-Real Multiply"); }
az = a + b*i;
bz = b + a*i;
cz = [-18+141*i , -27+162*i , -36+183*i ;
9+240*i , 0+279*i , -9+318*i ;
36+339*i , 27+396*i , 18+453*i ];
czz = [ 48+30*i , 54+36*i , 60+42*i ;
111+66*i , 126+81*i , 141+96*i ;
174+102*i, 198+126*i , 222+150*i ];
czzz = [ 48+111*i , 54+126*i , 60+141*i ;
111+174*i , 126+198*i , 141+222*i ;
174+237*i , 198+270*i , 222+303*i ];
if (any (any (cz != az*bz))) { error ("failed Complex-Complex Multiply"); }
if (any (any (czz != a*bz))) { error ("failed Real-Complex Multiply"); }
if (any (any (czzz != az*b))) { error ("failed Complex-Real Multiply"); }
a = [a,a];
b = [b;b];
c = [ 96 , 108 , 120 ;
222 , 252 , 282 ;
348 , 396 , 444 ];
if (any (any (c != a*b))) { error ("failed Real-Real Multiply"); }
az = [az,az];
bz = [bz;bz];
cz = [ -36+282*i , -54+324*i , -72+366*i ;
18+480*i , 0+558*i , -18+636*i ;
72+678*i , 54+792*i , 36+906*i ];
czz = [ 96+60*i , 108+72*i , 120+84*i ;
222+132*i , 252+162*i , 282+192*i ;
348+204*i , 396+252*i , 444+300*i ];
czzz = [ 96+222*i , 108+252*i , 120+282*i ;
222+348*i , 252+396*i , 282+444*i ;
348+474*i , 396+540*i , 444+606*i ];
if (any (any (cz != az*bz))) { error ("failed Complex-Complex Multiply"); }
if (any (any (czz != a*bz))) { error ("failed Real-Complex Multiply"); }
if (any (any (czzz != az*b))) { error ("failed Complex-Real Multiply"); }
printf("\t\tpassed matrix multiply test...\n");
//--------------------- Test STRING MATRICES --------------------------
//
printf("\t\tstring-matrices\n");
sm = ["s1","sm2","sm3";"x1","x2","xxx3";"y1","y2","yyy3"];
if(sm[1] != "s1") { error(); }
if( sm[1;3] != "sm3" ) { error(); }
if(any(sm[2,3;3] != ["xxx3";"yyy3"]) ) { error(); }
if(any (any ((sm[1;1]="xx")!=["xx","sm2","sm3";"x1","x2","xxx3";"y1","y2","yyy3"])))
{
error();
}
if( ((strm[1] = "strm")[1]) != "strm" ) { error(); }
// Test string-matrix add functionality
sm = sm[1,2;1,2];
if (any (any (("1_"+sm+"_2") != ["1_xx_2","1_sm2_2";"1_x1_2","1_x2_2"]))) {error();}
if ("c"+["1"] != "c1") { error (); }
if (["c"]+"1" != "c1") { error (); }
//
//---------------------------- List Tests --------------------------
//
// List creation
listest = << << 11; 12 >>; << 21; 22>> >>;
if( listest.[1].[2] != 12 ) { error(); }
if(any(<<a=10;b=1:4;c=[1,2,3;4,5,6;7,8,9]>>.b != [1,2,3,4])) { error(); }
mlist.[0] = m;
if(any(any(mlist.[0] != m))) { error(); }
printf("\tlist tests\n");
//
// Reset random number generator seed...
//
rand("default");
//
//-------------------------- Test abs () ------------------------------
//
A = rand(5,5);
T = ( A == abs (A));
if (!all (all (A))) { error ("abs() incorrect"); }
printf("\tabs()");
//
//-------------------------- Test max () ------------------------------
//
A = [1,10,100;2,20,200;1,2,3];
B = A./2;
ZA = A + rand (3,3)*A*1i;
ZB = B + rand (3,3)*B*1i;
if (!all (max (A) == [2,20,200])) { error( "max() incorrect"); }
if (max (max(A)) != 200) { error ("max() incorrect"); }
if (any (any (max (A, B) != A))) { error (); }
if (any (any (max (B, A) != max (A, B)))) { error (); }
if (any (any (max (ZB, ZA) != max (ZA, ZB)))) { error (); }
if (any (any (max (B, ZA) != max (ZA, B)))) { error (); }
if (any (any (max (ZB, A) != max (A, ZB)))) { error (); }
printf("\tmax()");
//
//-------------------------- Test min () ------------------------------
//
if (!all (min (A) == [1,2,3])) { error( "min() incorrect"); }
if (min (min(A)) != 1) { error ("min() incorrect"); }
if (any (any (min (A, B) != B))) { error (); }
if (any (any (min (B, A) != min (A, B)))) { error (); }
if (any (any (min (ZB, ZA) != min (ZA, ZB)))) { error (); }
if (any (any (min (B, ZA) != min (ZA, B)))) { error (); }
if (any (any (min (ZB, A) != min (A, ZB)))) { error (); }
printf("\tmin()");
//
//-------------------------- Test maxi () -----------------------------
//
if (!all (maxi (A) == [2,2,2])) { error( "maxi() incorrect"); }
if (maxi (maxi(A)) != 1) { error ("maxi() incorrect"); }
printf("\tmaxi()");
//
//-------------------------- Test mini () -----------------------------
//
if (!all (mini (A) == [1,3,3])) { error( "mini() incorrect"); }
if (mini (mini(A)) != 1) { error ("mini() incorrect"); }
printf("\tmini()");
//
//-------------------------- Test floor () ----------------------------
//
if (floor (1.9999) != 1) { error ("floor() output incorrect"); }
if (!all (all (floor ([1.99,1.99;2.99,2.99]) == [1,1;2,2]))) {
error ("floor output incorrect");
}
printf("\tfloor()");
//
//-------------------------- Test ceil () 0----------------------------
//
if (ceil (1.9999) != 2) { error ("ceil() output incorrect"); }
if (!all (all (ceil ([1.99,1.99;2.99,2.99]) == [2,2;3,3]))) {
error ("ceil output incorrect");
}
printf("\tceil()\n");
//
//-------------------------- Test round () ----------------------------
//
if (round (1.8) != 2) { error ("round() output incorrect"); }
if (round (1.4) != 1) { error ("round() output incorrect"); }
if (!all (all (round ([1.99,1.99;2.4,2.4]) == [2,2;2,2]))) {
error ("round output incorrect");
}
printf("\tround()");
//
//-------------------------- Test sum () ------------------------------
//
S = [1:4; 4:7; 8:11];
if (sum (S[1;]) != 10) { error ("sum() incorrect"); }
if (sum (S[3;]) != 38) { error ("sum() incorrect"); }
if (!all (all (sum (S) == [13,16,19,22]))) { error ("sum() incorrect"); }
printf("\tsum()");
//
//-------------------------- Test int () ------------------------------
//
if (int (1.9999) != 1) { error ("int() output incorrect"); }
if (!all (all (int ([1.99,1.99;2.99,2.99]) == [1,1;2,2]))) {
error ("int() output incorrect");
}
printf("\tint()");
//
//-------------------------- Test mod () ------------------------------
//
if (mod (1,1) != 0) { error ("mod() output incorrect"); }
if (mod (4,2) != 0) { error ("mod() output incorrect"); }
if (mod (3,2) != 1) { error ("mod() output incorrect"); }
if (mod (5,3) != 2) { error ("mod() output incorrect"); }
printf("\tmod()");
//
//-------------------------- Test find () ------------------------------
//
if (find ([0,1]) != 2) { error ("find() output incorrect"); }
if (find ([1,0]) != 1) { error ("find() output incorrect"); }
if (find ([0,1+1i]) != 2) { error ("find() output incorrect"); }
if (find ([1+0i,0]) != 1) { error ("find() output incorrect"); }
if (find ([0+1i,0]) != 1) { error ("find() output incorrect"); }
printf("\tfind()\n");
//
//-------------------------- Test rand () -----------------------------
//
rand ("normal", 5, 1);
xrand = rand(4000,1);
mean = function(x)
{
local(m);
m = size (x)[1];
if( m == 1 )
{
m = size (x)[2];
}
return sum( x ) / m;
};
std = function(x)
{
local(i, m, s);
if(class(x) != "num") { error("std() requires NUMERICAL input"); }
m = x.nr;
if( m == 1 )
{
return sqrt( sum( (x - mean(x)) .^ 2 ) / (x.nc - 1) );
else
for( i in 1:x.nc) {
s[i] = sqrt( sum( (x[;i] - mean(x[;i])) .^ 2 ) / (x.nr - 1) );
}
return s;
}
};
if (!(mean (xrand) > 4.9 && mean (xrand) < 5.1))
{ error ("error in random"); }
if (!(std (xrand) > 0.9 && std (xrand) < 1.1))
{ error ("error in random"); }
printf("\tpassed rand test...\n");
rand("default");
//
//-------------------------- Test norm () -----------------------------
//
tn = [1,2,3,4;2,1,2,3;3,2,1,2;4,3,2,1 ];
if (norm(tn,"m") != 4 ) { error ("incorrect norm computation"); }
if (norm(tn,"1") != 10) { error ("incorrect norm computation"); }
if (norm(tn,"i") != 10) { error ("incorrect norm computation"); }
printf("\tpassed norm test...\n");
//
//-------------------------- Test qr () ------------------------------
//
a = rand (4, 4);
qa = qr (a);
if (max (max (abs (qa.q*qa.r - a)))/(X*norm (a)*a.nr) > eps)
{ error ("possible qr() problems"); }
z = rand (4,4) + rand(4,4)*1i;
qz = qr (z);
if (max (max (abs (qz.q*qz.r - z)))/(X*norm (z)*z.nr) > eps)
{ error ("possible qr() problems"); }
printf("\tpassed qr test...\n");
//
//-------------------------- Test schur () ----------------------------
//
a = rand (4, 4);
sa = schur (a);
if (max (max (abs (sa.z*sa.t*sa.z' - a)))/(X*norm (a)*a.nr) > eps)
{ error ("possible schur() problems"); }
z = rand (4,4) + rand(4,4)*1i;
sz = schur (z);
if (max (max (abs (sz.z*sz.t*sz.z' - z)))/(X*norm (z)*z.nr) > eps)
{ error ("possible schur() problems"); }
printf("\tpassed schur test...\n");
//
//-------------------------- Test schord () ----------------------------
//
s2a = schord (sa, 2, 4);
if (max (max (abs (s2a.z*s2a.t*s2a.z' - a)))/(X*norm (a)*a.nr) > eps)
{ error ("possible schord() problems"); }
s2z = schord (sz, 3, 1);
if (max (max (abs (s2z.z*s2z.t*s2z.z' - z)))/(X*norm (z)*z.nr) > eps)
{ error ("possible schord() problems"); }
printf("\tpassed schord test...\n");
//
//-------------------------- Test chol () -----------------------------
//
c = rand(4,4);
c = symm (c + eye (size (c))*norm(c)*c.nr);
u = chol (c);
if (max (max (abs (u'*u - c)))/(X*norm (c)*c.nr) > eps)
{ error ("possible chol() problems"); }
cz = rand(4,4);
cz = symm (cz + eye (size (cz))*X*norm(cz)*cz.nr);
uz = chol (cz);
if (max (max (abs (uz'*uz - cz)))/(X*norm (cz)*cz.nr) > eps)
{ error ("possible chol() problems"); }
printf("\tpassed chol test...\n");
//
//-------------------------- Test solve () -----------------------------
//
a = rand(4,4);
while (1/rcond (a) > 100) { a = rand(4,4); }
b = ones(4,1);
x = solve (a,b);
if (max (max (abs(a*x - b)))/(X*norm (a)*a.nr) > eps)
{
printf ("\tThe condition # of a: %d\n", 1/rcond (a));
printf ("\tA*X - B:\n");
abs (a*x - b)
error ("possible solve() problems\n");
}
az = rand(4,4) + rand(4,4)*1j;
while (1/rcond (az) > 100) { az = rand(4,4) + rand(4,4)*1j; }
bz = rand(4,1) + rand(4,1)*1j;
xz = solve (az,bz);
if (max (max (abs (az*xz - bz)))/(X*norm (az)*az.nr) > eps)
{
printf ("\tThe condition # of z: %d\n", 1/rcond (az));
printf ("\tA*X - B:\n");
abs (az*xz - bz)
error ("possible solve() problems\n");
}
printf("\tpassed solve test...\n");
//
//-------------------------- Test lu() / factor() -----------------------------
//
tril = function(x, k)
{
local(i, j, nr, nc, y);
if (!exist (k)) { k = 0; }
nr = x.nr; nc = x.nc;
if(k > 0)
{
if (k > (nc - 1)) { error ("tril: invalid value for k"); }
else
if (abs (k) > (nr - 1)) { error ("tril: invalid value for k"); }
}
y = zeros(nr, nc);
for(i in max( [1,1-k] ):nr) {
j = 1:min( [nc, i+k] );
y[i;j] = x[i;j];
}
return y;
};
triu = function(x, k)
{
local(i, j, nr, nc, y);
if (!exist (k)) { k = 0; }
nr = x.nr; nc = x.nc;
if(k > 0)
{
if (k > (nc - 1)) { error ("triu: invalid value for k"); }
else
if (abs (k) > (nr - 1)) { error ("triu: invalid value for k"); }
}
y = zeros(nr, nc);
for(j in max( [1,1+k] ):nc) {
i = 1:min( [nr, j-k] );
y[i;j] = x[i;j];
}
return y;
};
static (swap);
lu = function ( A )
{
local (i, l, u, pvt, x)
if (A.nr != A.nc) { error ("lu() requires square A"); }
x = factor (A); // Do the factorization
//
// Now create l, u, and pvt from lu and pvt.
//
l = tril (x.lu, -1) + eye (size (x.lu));
u = triu (x.lu);
pvt = eye (size (x.lu));
//
// Now re-arange the columns of pvt
//
for (i in 1:max (size (x.lu)))
{
pvt = pvt[ ; swap (1:pvt.nc, i, x.pvt[i]) ];
}
return << l = l; u = u; pvt = pvt >>;
};
//
// In vector V, swap elements I, J
//
swap = function ( V, I, J )
{
local (v, tmp);
v = V;
tmp = v[I];
v[I] = v[J];
v[J] = tmp;
return v;
};
a = rand(4,4);
while (1/rcond (a) > 100) { a = rand(4,4); }
lua = lu (a);
if (max (max (abs(a - lua.pvt*lua.l*lua.u)))/(X*norm (a)*a.nr) > eps)
{
printf ("\tThe condition # of a: %d\n", 1/rcond (a));
printf ("\tA - p*l*u:\n");
abs (a - lua.pvt*lua.l*lua.u)
error ("possible lu()/factor() problems\n");
}
az = rand(4,4) + rand(4,4)*1j;
while (1/rcond (az) > 100) { az = rand(4,4) + rand(4,4)*1j; }
luz = lu (az);
if (max (max (abs (az - luz.pvt*luz.l*luz.u)))/(X*norm (az)*az.nr) > eps)
{
printf ("\tThe condition # of z: %d\n", 1/rcond (az));
printf ("\tA - p*l*u:\n");
abs (az - luz.ovt*luz.l*luz.u)
error ("possible lu()/factor()() problems\n");
}
printf("\tpassed lu/factor test...\n");
//
//-------------------------- Test svd () -----------------------------
//
a = rand(4,4);
s = svd (a);
if (max (max (abs (s.u*diag(s.sigma)*s.vt - a)))/(X*norm (a)*a.nr) > eps)
{ error ("possible svd() problems"); }
z = rand(4,4) + rand(4,4)*1j;
sz = svd (z);
if (max (max (abs (sz.u*diag(sz.sigma)*sz.vt - z)))/(X*norm (z)*z.nr) > eps)
{ error ("possible svd() problems"); }
printf("\tpassed svd test...\n");
//
//-------------------------- Test hess () -----------------------------
//
a = rand(4,4);
h = hess (a);
if (max (max (abs (h.p*h.h*h.p' - a)))/(X*norm (a)*a.nr) > eps)
{ error ("possible hess() problems"); }
z = rand(4,4) + rand(4,4)*1j;
hz = hess (z);
if (max (max (abs (hz.p*hz.h*hz.p' - z)))/(X*norm (z)*z.nr) > eps)
{ error ("possible hess() problems"); }
printf("\tpassed hess test...\n");
//
//-------------------------- Test lyap () ------------------------------
//
lyap = function ( A, B, C )
{
local (X, sa, sb, tc)
if (!exist (B))
{
B = A'; // Solve the special form: A*X + X*A' = -C
}
if ((A.nr != A.nc) || (B.nr != B.nc) || (C.nr != A.nr) || (C.nc != B.nr)) {
error ("Dimensions do not agree.");
}
//
// Schur decomposition on A and B
//
sa = schur (A);
sb = schur (B);
//
// transform C
//
tc = sa.z' * C * sb.z;
X = sylv (sa.t, sb.t, tc);
//
// Undo the transformation
//
X = sa.z * X * sb.z';
return X;
};
a = rand (5,5);
b = rand (5,5);
c = rand (5,5);
while (1/rcond (a) > 100) { a = rand(5,5); }
while (1/rcond (b) > 100) { b = rand(5,5); }
while (1/rcond (c) > 100) { c = rand(5,5); }
x = lyap (a, b, c);
if (max (max (abs (a*x + x*b + c)))/(X*norm(c)*norm(a)*norm(b)) > eps)
{ error ("possible problems with lyap() or sylv()"); }
printf("\tpassed lyap test...\n");
//
//-------------------------- Test eig () ------------------------------
//
trace = function(m)
{
local(i, tr);
if(m.class != "num") {
error("must provide NUMERICAL input to trace()");
}
tr = 0;
for(i in 1:min( [m.nr, m.nc] )) {
tr = tr + m[i;i];
}
return tr;
};
eye = function( m , n )
{
local(i, N, new);
if (!exist (n))
{
if(m.n != 2) { error("only 2-el MATRIX allowed as eye() arg"); }
new = zeros (m[1], m[2]);
N = min ([m[1], m[2]]);
else
if (class (m) == "string" || class (n) == "string") {
error ("eye(), string arguments not allowed");
}
if (max (size (m)) == 1 && max (size (n)) == 1)
{
new = zeros (m[1], n[1]);
N = min ([m[1], n[1]]);
else
error ("matrix arguments to eye() must be 1x1");
}
}
for(i in 1:N)
{
new[i;i] = 1.0;
}
return new;
};
//
// Standard eigenvalue problem
//
a = rand(10,10);
sa = symm (a);
ta = trace (a);
tsa = trace (sa);
z = rand(10,10) + rand(10,10)*1i;
sz = symm (z);
tz = trace (z);
tsz = trace (sz);
tol = 1.e-7;
if (!(ta < sum(eig(a).val) + tol && ta > sum(eig(a).val) - tol))
{
error ("error in eig");
}
if (!(tsa < sum(eig(sa).val) + tol && tsa > sum(eig(sa).val) - tol))
{
error ("error in eig");
}
if (!(tz < sum(eig(z).val) + tol && tz > sum(eig(z).val) - tol))
{
error ("error in eig");
}
if (!(tsz < sum(eig(sz).val) + tol && tsz > sum(eig(sz).val) - tol))
{
error ("error in eig");
}
//
// Generalized eigenvalue problem
//
b = rand(10,10);
sb = symm (b) + eye(size(b))*3;
tb = trace (b);
tsb = trace (sb);
zb = rand(10,10) + rand(10,10)*1i;
szb = symm (zb) + eye(size(zb))*3;
tzb = trace (zb);
tszb = trace (szb);
eig(a,b); // not sure of a good way to check these yet
eigs(sa,sb);
eigs(sa,sb);
eig(z, zb);
eigs(sz, szb);
eigs(sz, szb);
printf("\tpassed eig test...\n");
//
//-------------------------- Test fft () -----------------------------
//
if (100 != fft(ones(100,1))[1]) { error ("error in fft()"); }
printf("\tpassed fft test...\n");
//
//-------------------------- Test printf () --------------------------
//
sprintf (tmp, "%*.*d %*.*d %s %*.*f f\n", 5,3,2, 8,7,3, "string", 3, 4, 1234e-2);
if (!(tmp == " 002 0000003 string 12.3400 f\n")) { error ("sprintf() error"); }
printf("\tpassed sprintf test...\n");
//
//------------------------- Test strtod() ----------------------------
//
if (123.456 != strtod ("123.456")) { error (); }
if (!all (all ([1,2;3,4] == strtod (["1","2";"3","4"]))))
{ error (); }
printf("\tpassed strtod test...\n");
//
//------------------------- Test getline() ---------------------------
//
//
close( "test.getline" );
x = getline( "test.getline" );
if ( x.[1] != 123.456 ) { error(); }
if ( x.[2] != -123.456 ) { error(); }
if ( x.[3] != 123.456 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != .123 ) { error(); }
if ( x.[2] != -.123 ) { error(); }
if ( x.[3] != .123 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != 123 ) { error(); }
if ( x.[2] != -123 ) { error(); }
if ( x.[3] != 123 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != 1e6 ) { error(); }
if ( x.[2] != -1e6 ) { error(); }
if ( x.[3] != 1e6 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != 1e5 ) { error(); }
if ( x.[2] != -1e5 ) { error(); }
if ( x.[3] != 1e5 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != 123.456e3 ) { error(); }
if ( x.[2] != -123.456e3 ) { error(); }
if ( x.[3] != 123.456e3 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != 123.456e3 ) { error(); }
if ( x.[2] != -123.456e3 ) { error(); }
if ( x.[3] != 123.456e3 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != 123.456e-3 ) { error(); }
if ( x.[2] != -123.456e-3 ) { error(); }
if ( x.[3] != 123.456e-3 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != .123e3 ) { error(); }
if ( x.[2] != -.123e3 ) { error(); }
if ( x.[3] != .123e3 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != 123e3 ) { error(); }
if ( x.[2] != -123e3 ) { error(); }
if ( x.[3] != 123e3 ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != "123abc" ) { error(); }
if ( x.[2] != "abc123" ) { error(); }
if ( x.[3] != "123.abc" ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != "quoted string" ) { error(); }
if ( x.[2] != "q string with escapes \n \t \" " ) { error(); }
x = getline( "test.getline" );
if ( x.[1] != "quoted string" ) { error(); }
if ( x.[2] != 1.23e3 ) { error(); }
if ( x.[3] != 100 ) { error(); }
if ( x.[4] != "q string with escapes \n \t \" " ) { error(); }
if ( x.[5] != 200 ) { error(); }
printf("\tpassed getline() test...\n");
//
//---------------------- Test readb()/writeb() --------------------
//
a = rand (5,5);
z = rand(3,3) + rand(3,3)*1j;
strm = what()[1:5;1:5];
pi = 4*atan(1.0);
sc = 2*pi;
str = "this is a sample string\ttab";
l = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
writeb ("jnk.rb", a, z, strm, sc, str, l);
# Set aside matrices for later tests
check = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
clear (a, z, strm, sc, str, l);
close ("jnk.rb");
readb ("jnk.rb");
#
# Now do checks
#
if (!all (all (a == check.a))) { error (); }
if (!all (all (z == check.z))) { error (); }
if (!all (all (strm == check.strm))) { error (); }
if (sc != check.sc) { error (); }
if (str != check.str) { error (); }
if (length (l) != 5) { error (); }
if (!all (all (l.a == check.a))) { error (); }
if (!all (all (l.z == check.z))) { error (); }
if (!all (all (l.strm == check.strm))) { error (); }
if (l.sc != check.sc) { error (); }
if (l.str != check.str) { error (); }
printf("\tpassed binary I/O test...\n");
//
//---------------------- Test read()/write() --------------------
//
a = rand (5,5);
z = rand(3,3) + rand(3,3)*1j;
strm = what()[1:5;1:5];
pi = 4*atan(1.0);
sc = 2*pi;
str = "this is a sample string\ttab";
l = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
write ("jnk.ra", a, z, strm, sc, str, l);
# Set aside matrices for later tests
check = <<a = a; z = z; strm = strm; sc = sc; str = str>>;
clear (a, z, strm, sc, str, l);
close ("jnk.ra");
read ("jnk.ra");
#
# Now do checks
#
if (a.nr != 5 || a.nc != 5) { error (); }
if (z.nr != 3 || z.nc != 3) { error (); }
if (strm.nr != 5 || strm.nc != 5) { error (); }
if (str != check.str) { error (); }
if (length (l) != 5) { error (); }
if (l.a.nr != 5 || l.a.nc != 5) { error (); }
if (l.z.nr != 3 || l.z.nc != 3) { error (); }
if (l.strm.nr != 5 || l.strm.nc != 5) { error (); }
if (l.str != check.str) { error (); }
printf("\tpassed ascii I/O test...\n");
//
//------------------------- Fibonacci Test -------------------------
//
// Calculate Fibonacci numbers
//
i=1;
while ( i < 2 ) {
i=i+1;
a=0; b=1;
while ( b < 10000 ) {
c = b;
b = a+b;
a = c;
}
}
if ( b != 10946 ) {
error("failed fibonacci test");
else
printf("\tpassed fibonacci test...\n");
}
//
//------------------------- Factorial Test -------------------------
//
fac = function(a) {
if(a <= 1) {
return 1
else
return a*fac(a-1)
}
};
if(fac(10) != 3628800) { error(); else printf("\tpassed factorial test...\n"); }
//
//--------------------------- ACK Test ----------------------------
//
ack = function(a, b) {
if(a == 0) { return b + 1; }
if(b == 0) { return $self(a-1, 1); }
return $self(a-1, $self(a, b-1));
};
if(ack(2,2) != 7) {
error("error in ack() test");
else
printf("\tpassed ACK test...\n");
}
//
//------------------------- Prime Test -----------------------------
//
// An example that finds all primes less than limit
//
primes = function (limit) {
local(prime, cnt, i, j, k);
i = 1; cnt = 0;
for(k in 2:limit) {
j = 2;
while(mod(k,j) != 0) {
j++;
}
if(j == k) { // Found prime
cnt++;
prime[i;1] = k;
i++;
}
}
return prime;
};
if(max(size(primes(100))) != 25) {
error("error in prime test");
else
printf("\tpassed prime test...\n");
}
//
//--------------------------- Fib Min Test -----------------------------
//
// fibmin() will minimize an arbitrary function
// in 1D using Fibonacci search
f065 = function(x)
{
return (x - 0.65) * (x - 0.65);
};
fib = function(x)
{
local (n, a, b);
a = 1;
b = 1;
if (x >= 2)
{
n = x - 1;
for (n in n:1:-1)
{
c = b;
b = a + b;
a = c;
n = n - 1;
}
}
return b;
};
// Minimize a 1D function using Fibonacci search
// f = function to minimize
// xlo = lower bound
// xhi = upper bound
// n = number of iterations (the bigger the more accurate)
fibmin = function(f, xlo, xhi, n) {
local(a, b, x, y, ex, ey, k, lo, hi);
lo = xlo;
hi = xhi;
k = n;
for (k in k:2:-1)
{
a = fib(k - 2) / fib(k);
b = fib(k - 1) / fib(k);
x = lo + (hi - lo) * a;
y = lo + (hi - lo) * b;
ex = f(x);
ey = f(y);
if (ex >= ey)
{
lo = x;
else
hi = y;
}
// printf("%d: (%g %g) %g %g %g %g\n", k, a, b, lo, hi, ex, ey);
}
return (lo + hi) / 2;
};
//
// Simple example using above function to mimize f065. Answer is 0.65
//
x = fibmin(f065, 0, 1, 30); // printf("f(%g)=%g\n", x, f065(x));
if (abs(x - 0.65) > 1e-6)
{
printf("x = %f\n", x);
error("failed fibmin test");
}
printf("\tpassed fibmin test...\n");
//
//--------------------- Nasty Function Test ------------------------
//
printf("\tStarting Nasty Function Test...");
printf("\tthis will take awhile\n");
check = function( a, b, c, d, e, f, g, h ) {
if ( a+b+c+d == e+f+g+h && ...
a^2+b^2+c^2+d^2 == e^2+f^2+g^2+h^2 && ...
a^3+b^3+c^3+d^3 == e^3+f^3+g^3+h^3 ) {
return 1;
else
return 0;
}
};
for(a in 8:10) {
for(b in 7:(a-1)) {
for(c in 6:(b-1)) {
for(d in 5:(c-1)) {
for(e in 4:(d-1)) {
for(f in 3:(e-1)) {
for(g in 2:(f-1)) {
for(h in 1:(g-1)) {
if(check( a, b, c, d, e, f, g, h ) || ...
check( a, e, c, d, b, f, g, h ) || ...
check( a, f, c, d, e, b, g, h ) || ...
check( a, g, c, d, e, f, b, h ) || ...
check( a, h, c, d, e, f, g, b ) || ...
check( a, b, e, d, c, f, g, h ) || ...
check( a, b, f, d, e, c, g, h ) || ...
check( a, b, g, d, e, f, c, h ) || ...
check( a, b, h, d, e, f, g, c ) || ...
check( a, b, c, e, d, f, g, h ) || ...
check( a, b, c, f, e, d, g, h ) || ...
check( a, b, c, g, e, f, d, h ) || ...
check( a, b, c, h, e, f, g, d ) || ...
check( a, e, f, d, b, c, g, h ) || ...
check( a, e, g, d, b, f, c, h ) || ...
check( a, e, h, d, b, f, g, c ) || ...
check( a, f, g, d, e, b, c, h ) || ...
check( a, f, h, d, e, b, g, c ) || ...
check( a, g, h, d, e, f, b, c ) || ...
check( a, b, e, f, c, d, g, h ) || ...
check( a, b, e, g, c, f, d, h ) || ...
check( a, b, e, h, c, f, g, d ) || ...
check( a, b, f, g, e, c, d, h ) || ...
check( a, b, f, h, e, c, g, d ) || ...
check( a, b, g, h, e, f, c, d ) || ...
check( a, e, f, g, e, f, g, h ) || ...
check( a, e, f, h, e, f, g, h ) || ...
check( a, e, g, h, e, f, g, h ) || ...
check( a, f, g, h, e, f, g, h ) ) { cnt++; }
}
}
}
}
}
}
}
}
if(1) { // figure out the value of cnt, and check!
printf("\tpassed nasty function test...\n");
else
error();
}
//
//------------------ Test More Advanced Functions --------------------
//
printf( "\tStarting the lqr/ode test..." );
printf( "\tthis will take awhile\n" );
lqr = function( a, b, q, r ) {
local( k, s,...
m, n, mb, nb, mq, nq,...
e, v, d );
m = size(a)[1]; n = size(a)[2];
mb = size(b)[1]; nb = size(b)[2];
mq = size(q)[1]; nq = size(q)[2];
if ( m != mq || n != nq ) {
fprintf( "stderr", "A and Q must be the same size.\n" );
quit
}
mr = size(r)[1]; nr = size(r)[2];
if ( mr != nr || nb != mr ) {
fprintf( "stderr", "B and R must be consistent.\n" );
quit
}
nn = zeros( m, nb );
// Start eigenvector decomposition by finding eigenvectors of Hamiltonian:
e = eig( [ a, solve(r',b')'*b'; q, -a' ] );
v = e.vec; d = e.val;
index = sort( real( d ) ).ind;
d = real( d[ index ] );
if ( !( d[n] < 0 && d[n+1] > 0 ) ) {
fprintf( "stderr", "Can't order eigenvalues.\n" );
quit
}
chi = v[ 1:n; index[1:n] ];
lambda = v[ (n+1):(2*n); index[1:n] ];
s = -real(solve(chi',lambda')');
k = solve( r, nn'+b'*s );
return << k=k; s=s >>;
};
// Now run a little test problem.
k = 1; m = 1; c = .1;
a = [0 ,1 ,0 , 0;
-k/m, -c/m, k/m, c/m;
0, 0, 0, 1;
k/m, c/m, -k/m, -c/m ];
b = [ 0; 1/m; 0; 0 ];
qxx = diag( [0, 0, 100, 0] );
ruu = [1];
K = lqr( a, b, qxx, ruu ).k;
dot = function( t, x )
{
return (a-b*K)*x + b*K*([1,0,1,0]');
};
x = ode ( dot, 0, 15, [0,0,0,0], .02, 1e-5, 1e-5 );
m = maxi( x[;2] );
if ( (abs( x[m;2] - 1.195 ) > 0.001) || ...
any (abs( x[x.nr;2,4] - 1 ) > 0.001) ) {
printf( "\tfailed***\n" );
else
printf( "\tpassed the lqr/ode test...\n" );
}
printf("Elapsed time = %i seconds\n", toc() );
"FINISHED TESTS"